package cm.sms.utils;

/**
 * @author: yxh
 * @Description: 阿里云
 * @Date: Created in 2022/9/28 13:31
 * @Modified By:
 */
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import com.aliyun.sdk.service.dysmsapi20170525.*;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;

/**
 * 阿里云异步短信发送-升级SDK
 */
@Slf4j
public class AliYunSendSmsUtil {
    /**
     * REGION, 使用杭州
     */
    private static final String ENDPOINT = "cn-beijing";

    private static final String accessKeyId = "LTAI5tCnKso2uG3kJ5gRav88";

    private static final String accessKeySecret = "fGJ5SNXL7P1NHNRmJ7DJaMJGPyE55C";

    private static final String endpointOverride = "dysmsapi.aliyuncs.com";


    public void sendSms() {
        try {
            StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                    .accessKeyId(accessKeyId)
                    .accessKeySecret(accessKeySecret)
                    .build());

            AsyncClient client = AsyncClient.builder()
                    .region(ENDPOINT)
                    .credentialsProvider(provider)
                    .overrideConfiguration(
                            ClientOverrideConfiguration.create()
                                    .setEndpointOverride(endpointOverride)
                    )
                    .build();

            SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                    .signName("Ballcat")
                    .templateCode("SMS_207945135")
                    .phoneNumbers("18815596836")
                    .templateParam("{\"code\":\"2561\"}")
                    .build();

            CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
            SendSmsResponse resp = response.get();
            System.out.println(new Gson().toJson(resp));
            client.close();
        }catch (Exception e){
            e.getMessage();
        }
    }

    public static void main(String[] args) {
        new AliYunSendSmsUtil().sendSms();
    }

}